<script>
import config from './config';
import { districtInitSql } from '@/utils/districtInitSql.js';
import store from '@/store';
import {tableSqls} from "@/utils/sqlite.js"
import {dbName} from "@/utils/sqlite.js"
import { getAccessToken } from '@/utils/auth';
import { onShow } from '@dcloudio/uni-app'
import dbUtils from '@/uni_modules/zjy-sqlite-manage/components/zjy-sqlite-manage/dbUtils'
import {errorKey} from "./utils/sqlite";
export default {
  data(){
    return {
    }
  },
	methods: {
    initTemp(){
// 检查缓存中是否已有数据
      uni.getStorage({
        key: errorKey,
        success: function (res) {
          console.log('缓存中已有数据:', res.data);
        },
        fail: function () {
          // 如果没有数据，则写入默认值
          uni.setStorage({
            key: errorKey,
            data: '100',
            success: function () {
              console.log('默认数据已写入缓存');
            }
          });
        }
      });

    },
		async listenToNetworkStatus() {
		  return new Promise((resolve, reject) => {
		    uni.onNetworkStatusChange((res) => {
		      if (!res.isConnected) {
		        reject(new Error('当前无网络连接'));
		      } else {
		        resolve(res);
		      }
		    });
		  });
		},
		initDistrict(){
		  districtInitSql(this.$dbUtils)
		},
		// 初始化数据库
		initDateBase(){
			dbUtils.openDb("tb_school_screen")
			dbUtils.init("tb_school_screen",tableSqls)
			// this.$dbUtils.closeSQL("th_screen")
		},
		// 初始化应用
		initApp() {
			// 初始化应用配置
			this.initConfig();
			// 检查用户登录状态
			//#ifdef H5
			this.checkLogin();
			//#endif
		},
		initConfig() {
			this.globalData.config = config;
		},
		checkLogin() {
			if (!getAccessToken()) {
				this.$tab.reLaunch('/pages/index');
			}
		}
	},
  onLaunch: function () {
    this.initApp();
    this.initDateBase()
    this.initTemp()
    // districtInitSql(this.$dbUtils)
    // this.initDistrict()
  },
  async onShow() {
    uni.onNetworkStatusChange((res) => {
      console.log(res);
      if (!res.isConnected) {
        uni.$netType = false
        uni.hideLoading();
        uni.showToast({
          title: '当前无网络连接',
          icon: 'none',
          duration: 2000
        })
      } else {

      }
    });
    this.initDateBase()
    // this.initDistrict()
  },
};
</script>

<style lang="scss">
/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
@import "@/uni_modules/uview-plus/index.scss";
@import '@/static/scss/index.scss';
</style>
